package com.rocsea.robot.template;
import com.rocsea.robot.model.bo.IdMaxTransRelated;
import com.rocsea.robot.model.entity.StepAttributeDO;
import com.rocsea.robot.model.entity.TransformationDO;
import com.rocsea.robot.model.bo.KpBuildInfo;
import com.rocsea.robot.utils.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

import static com.rocsea.robot.constant.KettleBuildConstant.*;
import static com.rocsea.robot.constant.SqlBuildConstant.SELECT_PREFIX;

/**
 * 抽象的构建模板
 * @Author RocSea
 * @Date 2023/1/30
 */
public abstract class AbstractTemplate implements Template {

    @Override
    public String processTransformation(TransformationDO transformationDO) {
        TransformationDO buildDO = BeanCopyUtils.copyProperties(transformationDO, TransformationDO.class);
        buildDO.setIdTransformation(KettleBuildUtils.getIdMaxTransRelated().addAndGetIdTransformationMax());
        buildDO.setIdDirectory(KettleBuildUtils.getIdDirectory());
        buildDO.setName(buildDO.getName().replace(this.getTemplateInfo().getName(), KettleBuildUtils.getSourceTableName()));
        return MysqlBuilderUtils.buildTransformation(buildDO);
    }

    public String processTableInputWithRowMeta(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        StringBuilder sql = new StringBuilder();
        IdMaxTransRelated idMaxTransRelated = KettleBuildUtils.getIdMaxTransRelated();
        for (StepAttributeDO t : stepAttributeDOS) {
            StepAttributeDO buildDO = BeanCopyUtils.copyProperties(t, StepAttributeDO.class);
            buildDO.setIdTransformation(idMaxTransRelated.getIdTransformationMax());
            buildDO.setIdStep(idMaxTransRelated.getIdStepMax());
            switch (t.getCode()) {
                case STEP_ATTRIBUTE_CODE_ROW_META:
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    String valueStr = KettleBuildUtils.of(KettleBuildUtils.getSourceColumnList()).getRowMetaXml();
                    buildDO.setValueStr(valueStr);
                    // 构建 r_step_attribute
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
                    break;
                case STEP_ATTRIBUTE_CODE_SQL:
                    String sqlSuffix = this.processSqlReplace(t.getValueStr());
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    if(KettleBuildUtils.getSelectColumn().contains("as update_time,")){
                        Optional<Map.Entry<String, String>> updateTimeOptional =
                                KettleBuildUtils.getAliasMapping().entrySet().stream().filter(x -> x.getValue().equals("update_time")).findFirst();
                        sqlSuffix = sqlSuffix.replace("update_time", updateTimeOptional.get().getKey());
                    }
                    buildDO.setValueStr(SELECT_PREFIX + KettleBuildUtils.getSelectColumn() + " " + sqlSuffix);
                    // 构建 r_step_attribute
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
                    break;
                case STEP_ATTRIBUTE_ID_CONNECTION:
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    buildDO.setValueNum(KettleBuildUtils.getIdConnection());
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
                    break;
                default:
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
            }
        }
        return sql.toString();
    }

    public String processTableInputNoRowMeta(List<StepAttributeDO> stepAttributeDOS) {
        IdMaxTransRelated idMaxTransRelated = KettleBuildUtils.getIdMaxTransRelated();
        StringBuilder sql = new StringBuilder();
        for (StepAttributeDO t : stepAttributeDOS) {
            StepAttributeDO buildDO = BeanCopyUtils.copyProperties(t, StepAttributeDO.class);
            buildDO.setIdTransformation(idMaxTransRelated.getIdTransformationMax());
            buildDO.setIdStep(idMaxTransRelated.getIdStepMax());
            switch (t.getCode()) {
                case STEP_ATTRIBUTE_CODE_SQL:
                    String sqlSuffix = this.processSqlReplace(t.getValueStr());
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    buildDO.setValueStr(SELECT_PREFIX + KettleBuildUtils.getSelectColumn() + " " + sqlSuffix);
                    // 构建 r_step_attribute
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
                    break;
                case STEP_ATTRIBUTE_ID_CONNECTION:
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    buildDO.setValueNum(KettleBuildUtils.getIdConnection());
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
                    break;
                default:
                    buildDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
                    sql.append(MysqlBuilderUtils.buildStepAttribute(buildDO));
            }
        }
        return sql.toString();
    }

    @Override
    public String processBatchInsertOrUpdate(List<StepAttributeDO> stepAttributeDOS) {
        IdMaxTransRelated idMaxTransRelated = KettleBuildUtils.getIdMaxTransRelated();
        StringBuilder sql = new StringBuilder();
        for (StepAttributeDO t : stepAttributeDOS) {
            String valueStr = t.getValueStr();
            StepAttributeDO stepAttributeDO = BeanCopyUtils.copyProperties(t, StepAttributeDO.class);
            stepAttributeDO.setIdStepAttribute(idMaxTransRelated.addAndGetIdStepAttributeMax());
            stepAttributeDO.setIdTransformation(idMaxTransRelated.getIdTransformationMax());
            stepAttributeDO.setIdStep(idMaxTransRelated.getIdStepMax());
            if (STEP_ATTRIBUTE_CODE_TRANS_NAME.equals(t.getCode())) {
                valueStr = valueStr.replace(this.getTemplateInfo().getName(), KettleBuildUtils.getSourceTableName());
            }
            stepAttributeDO.setValueStr(valueStr);
            sql.append(MysqlBuilderUtils.buildStepAttribute(stepAttributeDO));
        }
        return sql.toString();
    }

    public KpBuildInfo createKpBuildInfo(String outDatabase ) {
        String tableName = KettleBuildUtils.getSourceTableName();
        String sourceSchemaName = KettleBuildUtils.getSourceSchemaName();
        String targetTableName = KettleBuildUtils.getTargetTableName();
        String taskName = tableName;
        String jobName = tableName;
        if (StringUtils.isNotEmpty(outDatabase)) {
            taskName = sourceSchemaName + "." + tableName;
            String[] s = sourceSchemaName.split("_");
            jobName = s[0] + "." + tableName;
        }
        KpBuildInfo kpBuildInfo = new KpBuildInfo();
        kpBuildInfo.setTaskName(taskName);
        kpBuildInfo.setJobName(jobName);
        kpBuildInfo.setTableName(tableName);
        kpBuildInfo.setTargetTableName(targetTableName);
        return kpBuildInfo;
    }

}
